<template>
  <div class="mainpg">
    <div class="main_spac1">
      <div class="main_white padding10">
        <div class="mainor_form batch-wrap">
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="90px"
            id="form"
          >
            <el-form-item label="商品名称：" prop="goods_spu_id">
              <change-goods-input
                ref="changeGoodsInput"
                @clear="
                  () => {
                    this.form.goods_spu_id = '';
                  }
                "
                @sendValue="
                  (data) => {
                    this.form.goods_spu_id = data.id;
                  }
                "
              ></change-goods-input>
            </el-form-item>
            <el-form-item label="分类：" prop="category_id" class="wid180">
              <el-cascader
                v-model="form.category_id"
                :options="options"
                :props="defaultProps"
                @change="getArticleList"
              ></el-cascader>
            </el-form-item>
            <el-form-item label="状态：" prop="status">
              <el-select
                v-model.trim="form.status"
                placeholder="商品状态"
                class="wid180"
                @change="getArticleList"
              >
                <el-option label="所有" value="0"></el-option>
                <el-option
                  v-for="(statusList, index) in list.statusList"
                  :key="index"
                  :label="statusList"
                  :value="index + 1"
                ></el-option>
              </el-select>
            </el-form-item>
            <MainFormBtn @reset="reset" @getSearchFor="getArticleList" :loading="listLoading" :showBtn="false">
            </MainFormBtn>
          </el-form>
        </div>
        <div class="main_table mg-t-6" :style="{ height: borderHeight + 'px' }">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list.data"
            element-loading-text="加载中"
            fit
            highlight-current-row
            :height="borderHeight"
          >
            <el-table-column
              label="商品名称"
              width="200"
              prop="goods.name"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope"
                >{{ scope.row.goods.name }} 【{{
                  scope.row.spu_name ? scope.row.spu_name.title : ""
                }}】</template
              >
            </el-table-column>
            <el-table-column
              label="单位"
              width="120"
              prop="spu_unit.title"
              :show-overflow-tooltip="true"
            >
              <template slot-scope="scope">
                {{ scope.row.spu_unit ? scope.row.spu_unit.title : "" }}
              </template>
            </el-table-column>
            <el-table-column label="图片" width="120" class-name="main_hei">
              <template slot-scope="scope">
                <div class="demo-image__error mainor_im">
                  <el-image
                    fit="fill"
                    :src="scope.row.pic"
                    :preview-src-list="scope.row.pics"
                  >
                    <div slot="error" class="image-slot">
                      <img :src="puzzleImg" />
                    </div>
                  </el-image>
                </div>
              </template>
            </el-table-column>
            <el-table-column
              label="长（cm）"
              :show-overflow-tooltip="true"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <div v-if="!scope.row.freight_le" class="mainus_pe">
                  {{ scope.row.length }}
                  <svg
                    aria-hidden="true"
                    class="main_page_svg"
                    width="13"
                    height="13"
                    @click="freighta(scope.$index, 'freight_le')"
                  >
                    <use xlink:href="#icon-commodity_modify" />
                  </svg>
                </div>
                <el-input
                  v-else
                  v-focus="'freightw' + scope.$index"
                  v-model="scope.row.length"
                  placeholder="采购价"
                  class="wid80"
                  @blur.prevent="
                    (val) => {
                      inputBlur(val, scope.$index, 'freight_le', 'length');
                    }
                  "
                  @keyup.enter.native="$event.target.blur"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column
              label="宽（cm）"
              :show-overflow-tooltip="true"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <div v-if="!scope.row.freight_wi" class="mainus_pe">
                  {{ scope.row.width }}
                  <svg
                    aria-hidden="true"
                    class="main_page_svg"
                    width="13"
                    height="13"
                    @click="freighta(scope.$index, 'freight_wi')"
                  >
                    <use xlink:href="#icon-commodity_modify" />
                  </svg>
                </div>
                <el-input
                  v-else
                  v-focus="'freightw' + scope.$index"
                  v-model="scope.row.width"
                  placeholder="库存价"
                  class="wid80"
                  @blur.prevent="
                    (val) => {
                      inputBlur(val, scope.$index, 'freight_wi', 'width');
                    }
                  "
                  @keyup.enter.native="$event.target.blur"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column
              label="高（cm）"
              :show-overflow-tooltip="true"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <div v-if="!scope.row.freight_he" class="mainus_pe">
                  {{ scope.row.height }}
                  <svg
                    aria-hidden="true"
                    class="main_page_svg"
                    width="13"
                    height="13"
                    @click="freighta(scope.$index, 'freight_he')"
                  >
                    <use xlink:href="#icon-commodity_modify" />
                  </svg>
                </div>
                <el-input
                  v-else
                  v-focus="'freightw' + scope.$index"
                  v-model="scope.row.height"
                  placeholder="库存价"
                  class="wid80"
                  @blur.prevent="
                    (val) => {
                      inputBlur(val, scope.$index, 'freight_he', 'height');
                    }
                  "
                  @keyup.enter.native="$event.target.blur"
                ></el-input>
              </template>
            </el-table-column>
            <el-table-column
              label="重量（kg）"
              :show-overflow-tooltip="true"
              class-name="main_hei"
            >
              <template slot-scope="scope">
                <div v-if="!scope.row.freight_we" class="mainus_pe">
                  {{ scope.row.weight }}
                  <svg
                    aria-hidden="true"
                    class="main_page_svg"
                    width="13"
                    height="13"
                    @click="freighta(scope.$index, 'freight_we')"
                  >
                    <use xlink:href="#icon-commodity_modify" />
                  </svg>
                </div>
                <el-input
                  v-else
                  v-focus="'freightw' + scope.$index"
                  v-model="scope.row.weight"
                  placeholder="库存价"
                  class="wid80"
                  @blur.prevent="
                    (val) => {
                      inputBlur(val, scope.$index, 'freight_we', 'weight');
                    }
                  "
                  @keyup.enter.native="$event.target.blur"
                ></el-input>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="main_page boxdq">
          <div></div>
          <!-- 分页 -->
          <div class="main_page_r">
            <MainPagination
              :current-page="current.page"
              :page-sizes="[20, 30, 50, 100]"
              :page-size="limit"
              layout="total, sizes, prev, pager, next, jumper"
              :total="totalCount"
              background
              @size-change="changeLimit"
              @current-change="changePage"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mixins } from "@/mixins/index.js";
import { mapGetters } from "vuex";
import { sku_index, sku_volume } from "@/api/commodity/commodity_Pricing";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { puzzleImg } from "@/api/currency";
import { recursion, category } from "@/utils/index";

export default {
  mixins: [mixins],
  computed: {
    ...mapGetters(["avatar"]),
  },
  directives: {
    // 聚焦元素
    focus: {
      inserted(el) {
        el.querySelector("input").focus();
      },
    },
  },
  data() {
    return {
      // 搜索
      form: {
        goods_spu_id: "",
        pid: "",
        status: null,
        category_id: [],
        is_all: 1,
      },
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: {
          data: [],
        },
      },
      puzzleImg,
      listLoading: false,
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
      // 焦点
      offsetParent: true,
    };
  },
  methods: {
    startGetting() {
      this.getArticleList();
    },
    // 点击排序
    freighta(index, name) {
      if (this.offsetParent) {
        this.list.list.data[index][name] = !this.list.list.data[index][name];
      }
    },
    // 焦点离开排序
    inputBlur(val, index, name, type) {
      this.offsetParent = false;
      if (
        /^(\d+\.?)?\d{0,2}$/.test(parseFloat(val.target.value)) &&
        val.target.value != ""
      ) {
        this.list.list.data[index][name] = !this.list.list.data[index][name];
        this.offsetParent = true;
        this.goods_change(
          val.target.value,
          this.list.list.data[index].id,
          type
        );
      } else {
        this.$nextTick(() => {
          if (val.target.offsetParent) {
            val.target.offsetParent.querySelector("input").focus();
          }
        });
      }
    },
    goods_change(val, id, type) {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, { id: id, type: type, value: val })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        { id: id, type: type, value: val }
      );
      sku_volume(data).then((res) => {
        if (res.errcode == 0) {
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.sku_index();
    },
    changePage(page) {
      this.current.page = page;
      this.sku_index();
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.sku_index();
    },
    reset() {
      this.$refs["changeGoodsInput"].name = "";
      this.$refs["userListFrom"].resetFields();
      this.sku_index();
    },
    // 列表
    sku_index() {
      this.listLoading = true;
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, form, this.current)) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        form,
        this.current
      );
      sku_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.options = recursion(res.data.categoryList, true);
          res.data.list.data.map((e) => {
            if (e.goods.images.length != 0) {
              e.goods.images.map((a) => {
                a.path = this.avatar.domain + a.path;
                if (a.main == 1) {
                  e.pic = a.path;
                }
              });
              e.pics = e.goods.images.map((s) => s.path);
            } else {
              e.pic = "";
              e.pics = [];
            }
            e.freight_le = false;
            e.freight_wi = false;
            e.freight_he = false;
            e.freight_we = false;
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
  },
  created() {
    // 列表
    this.getNumerical(225);
    this.sku_index();
  },
};
</script>
<style lang="scss" scoped>
.mainor_form form {
  width: calc(100% - 180px);
}
/deep/.el-form-item--small.el-form-item {
  margin-bottom: 10px;
}
</style>
